# -*- coding: UTF-8 -*-

import data.definitions
import datetime
import ui.windows

_controller = None

class Controller:
    ''' Main controller class for the application. See MVC Pattern.
        It is responsible for coordinating the flow of data between the view and
        the model, as well as responding to events on both fronts. It is able
        to receive notifications from the model via a callback mechanism. '''
    def __init__(self):
        self.calendar = data.definitions.Calendar()
        self.calendar.register_callback(calendar_callback)
        self.TimeLord = None
    
    def add_event(self, event):
        self.calendar.add_event(event)
    
    def refresh_grids(self):
        pass

    def refresh_event_grid(self):
        self.TimeLord.refresh_events()

    def get_events(self):
        # TODO: Store the current calendar day somewhere, shouldn't use today all the time
        return self.calendar.get_today_events()
    
    def create_window(self):
        import gettext
        gettext.install("TimeLord") # replace with the appropriate catalog name
        self.TimeLord = ui.windows.TimeApp(0)
        self.TimeLord.MainLoop()        

def calendar_callback():
    get_controller().refresh_event_grid()

def get_controller():
    global _controller
    if  _controller is None:
        _controller = Controller()
    return _controller
